package com.sktelecom.model;

import java.io.Serializable;

import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.Table;
import javax.xml.bind.annotation.XmlRootElement;
import javax.xml.bind.annotation.XmlTransient;

import org.apache.commons.lang.builder.ToStringBuilder;
import org.apache.commons.lang.builder.ToStringStyle;
import org.codehaus.jackson.annotate.JsonIgnore;
import org.hibernate.search.annotations.DocumentId;
import org.hibernate.search.annotations.Field;
import org.hibernate.search.annotations.Indexed;

/**
 * This class represents the basic "user" object in Demo that allows for authentication
 * and user management.  It implements Serializable interface.
 *
 * @author <a href="mailto:hangyong2@hotmail.com">hangyong.choi</a>
 *         Extended to implement Serializable interface
 */
@Entity
@Table(name = "board")
@Indexed
@XmlRootElement
public class Board extends BaseObject implements Serializable {
    private static final long serialVersionUID = 3832626162173359412L;

    private Long id;
    private String username;                    // required
    private String title;                    // required
    private String contents;                // required

    /**
     * Default constructor - creates a new instance with no values set.
     */
    public Board() {
    }

    /**
     * Create a new instance and set the title.
     *
     * @param title
     */
    public Board(final String title) {
        this.title = title;
    }

    @Id
    @GeneratedValue(strategy = GenerationType.AUTO)
    @DocumentId
    public Long getId() {
        return id;
    }

    @Column(nullable = false, length = 50)
    @Field
    public String getUsername() {
        return username;
    }

    @Column(nullable = false, length = 255)
    @XmlTransient
    @JsonIgnore
    public String getTitle() {
        return title;
    }


    @Column(nullable = false, length = 255)
    @XmlTransient
    public String getContents() {
        return contents;
    }

    public void setId(Long id) {
        this.id = id;
    }

    public void setUsername(String username) {
        this.username = username;
    }

    public void setTitle(String title) {
        this.title = title;
    }

    public void setContents(String contents) {
        this.contents = contents;
    }

    /**
     * {@inheritDoc}
     */
    public boolean equals(Object o) {
        if (this == o) {
            return true;
        }
        if (!(o instanceof User)) {
            return false;
        }

        final User user = (User) o;

        return !(username != null ? !username.equals(user.getUsername()) : user.getUsername() != null);

    }

    /**
     * {@inheritDoc}
     */
    public int hashCode() {
        return (username != null ? username.hashCode() : 0);
    }

    /**
     * {@inheritDoc}
     */
    public String toString() {
        ToStringBuilder sb = new ToStringBuilder(this, ToStringStyle.DEFAULT_STYLE)
                .append("username", this.username);

        sb.append("No Granted Authorities");
        return sb.toString();
    }
}
